package com.java2007.hotel.service.front.impl;

import com.java2007.hotel.constant.Constant;
import com.java2007.hotel.dao.front.UserDao;
import com.java2007.hotel.factory.BeanFactory;
import com.java2007.hotel.pojo.Admin;
import com.java2007.hotel.pojo.User;
import com.java2007.hotel.service.front.UserService;
import com.java2007.hotel.utils.MD5;

import java.sql.SQLException;

/**
 * 客户业务层实现类
 * @author csl
 * @version V1.0
 * @date 2021/01/25
 **/
public class UserServiceImpl implements UserService {
    private UserDao userDao = (UserDao) BeanFactory.getBean(Constant.USER_DAO_IMPL);
    /**
     *增加一个客户
     * @param user
     * @return
     */
    @Override
    public int add(User user) throws SQLException {
        user.setPwd(MD5.md5(user.getPwd()));
        int add = userDao.add(user);
        return add;
    }
    /**
     * 根据名字查询客户
     * @param name
     * @return
     */
    @Override
    public User selectByName(String name,String pwd) throws SQLException {
        System.out.println(MD5.md5(pwd));
        User user = userDao.selectByName(name);
        System.out.println(user.getPwd());
        if (user != null) {
            if (!user.getPwd().equals(MD5.md5(pwd))) {
                throw new RuntimeException("用户名或密码错误");
            }
        } else {
            throw new RuntimeException("用户名或密码错误");
        }
        return user;
    }

    /**
     * 根据id查询客户
     * @param id
     * @return
     */
    @Override
    public User selectById(int id) throws SQLException {
        return null;
    }
    /**
     * 根据id修改客户价钱
     * @param id
     * @return
     */
    @Override
    public int updateAccountById(int id, Long account) throws SQLException {
        return userDao.updateAccountById(id,account);
    }
}
